import comSpSkeleton from "../../../../engines/components/comSpSkeleton";
import { managers } from "../../../../engines/gameEngine";
import NormalLevelBase from "../../../../Game/Base/LevelBase";
import { INormalLevelStruct } from "../../../../Game/Interface";
import mainRoot from "../../../../Game/MainRoot";

const { ccclass, property } = cc._decorator;

export enum EDownTimeSwitch {
    OPEN,
    CLOSE
}

@ccclass
export default class levelFindSprieBase extends NormalLevelBase {

    @property({ type: cc.SpriteFrame, displayName: "所有答案的图片" })
    answerSprites: cc.SpriteFrame[] = [];

    @property({ type: cc.Node, displayName: "答案节点" })
    answerNode: cc.Node = null;

    @property({ type: cc.Node, displayName: "答案content" })
    answerContent: cc.Node = null;

    @property({ type: cc.Node, displayName: "放大节点" })
    AMPNode: cc.Node = null;
    @property({ displayName: "是否完成后需要归位" })
    isNeedResturn: boolean = true;

    @property({type: cc.Node, displayName: '浮空content'})
    floatContent: cc.Node = null;

    @property({type: cc.Enum(EDownTimeSwitch), displayName: '是否开启倒计时'})
    isOpenDownTime: EDownTimeSwitch = EDownTimeSwitch.CLOSE;

    @property({ displayName: "倒计时时间", visible() {
        return this.isOpenDownTime == EDownTimeSwitch.OPEN;
    } })
    countDownTime: number = 10;

    private countDown: boolean = true;
    private nowCountDownTime: number = 0;
    //private lab_countDown: cc.Label = null;



    async Init(info: INormalLevelStruct): Promise<void> {
        super.Init(info);
    }

    onLoad() {
        super.onLoad();
        this.nowCountDownTime = this.countDownTime;
        if (this.isOpenDownTime == EDownTimeSwitch.OPEN) {
            this.countDown = true;
            // this.node.getChildByName("lab_countDown").active = true;
            // this.lab_countDown = this.node.getChildByName("lab_countDown").getComponent(cc.Label);
        }
    }

    start() {
        super.start();
        this.init();
    }

    pauseGame(): void {

    }
    resumeGame(): void {

    }

    
    update(dt: number): void {
        if (this.isOpenDownTime == EDownTimeSwitch.CLOSE) return;
        if (!this.countDown) return;

        this.nowCountDownTime -= dt;
        this.showCountDownLab();
    }

    showCountDownLab() {
        // let time = Math.ceil(this.nowCountDownTime);
        // time = Math.max(0, time);

        // if (time <= 0) {
        //     this.countDown = false;
        // }

        // if (time.toString() != this.lab_countDown.string) {
        //     this.lab_countDown.string = `${time}`;

        //     this.lab_countDown.fontSize = time <= 9 ? 50 : 40;
        //     this.lab_countDown.node.color = time <= 5 ? cc.Color.RED : cc.Color.BLACK;

        //     if (time <= 5) {
        //         cc.tween(this.lab_countDown.node)
        //             .to(0.3, {scale: 1.3})
        //             .to(0.3, {scale: 1.0})
        //             .call(() => {
        //                 if (time <= 0) {
        //                     //失败
        //                     this.Lose();
        //                 }
        //             })
        //             .start();
        //     }
        // }
    }

    decreaseMoney(tag: number) {
        
    }


    touchScene(e: cc.Event.EventTouch): void {
        let pos: cc.Vec2 = e.getLocation();
        pos = this.node.convertToNodeSpaceAR(pos);
        // let node = cc.instantiate(this.md);
        // mainRoot.UIRoot.addChild(node);
        // node.setPosition(pos);
        // node.getComponent(comSpSkeleton).playAnimation("animation").start(false).onComplete(1, () => {
        //     node.removeFromParent();
        // });
    }

    addOneAnswer(tag: number) {
        let spriteFrame = this.answerSprites[tag];
        let node = cc.instantiate(this.answerNode);
        node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        this.answerContent.addChild(node);
    }


    addOneFloatAnswer(tag: number, pos: cc.Vec2) {
        

    }

    addOneFloatAnswerByNode(tag: number, node: cc.Node) {

    }


    winAni() {
        return new Promise<void>(resolve => {
            if (!this.isNeedResturn) {
                resolve();
            } else {
                let speed: number = 2;
                let time = Math.abs(this.AMPNode.scale - 1) / speed
                cc.tween(this.AMPNode).to(time, { scale: 1, x: 0, y: 0 }, { easing: cc.easing.backIn }).call(() => {
                    resolve();
                }).start();
            }
        })
    }

    loseAni() {
        return new Promise<void>(resolve => {
            resolve();
        })
    }

    init() {

    }

    playSound(name: string, overCb?: () => void) {
        managers().soundMgr.stopAllEffect();
        managers().soundMgr.playSound(name, overCb);
    }

    isPlaySound: boolean = false;
    currentOkNum: number = 0;

    @property({ displayName: "需要答对的数量" })
    needCount: number = 0;

    @property({displayName: '第一个answer坐标'})
    firstPos = cc.v2(-294, 89);

}
